package com.example.myblog.controller.admin;

import com.example.myblog.entity.Blog;
import com.example.myblog.entity.Columnist;
import com.example.myblog.entity.Tag;
import com.example.myblog.entity.vo.BlogVo;
import com.example.myblog.service.BlogService;
import com.example.myblog.service.TagService;
import com.example.myblog.service.TypeService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    @GetMapping("/blogs")
    public String blogs(Model model){
        Page<Blog> page = blogService.listBlog(1,null);
        System.out.println("====================================================================");
        for (Blog blog : page) {
            System.out.println(blog.getBlogTag());
        }
        model.addAttribute("page",page);
        model.addAttribute("types",typeService.list());
        return "admin/blogs";
    }
    @PostMapping("/blogs/search")
    public String search(Model model, BlogVo blogVo,int page,Boolean search){
        if(search) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("title", blogVo.getTitle());
            Columnist columnist = typeService.findById(Integer.parseInt(blogVo.getColumnist()));
            map.put("columnist",columnist.getColName());
            System.out.println("===========================================================");
            if (!blogVo.getIgnoreRec()) {
                map.put("recommendable", blogVo.getRecommendable());
            }
            System.out.println(blogVo);
            model.addAttribute("page", blogService.listByCondition(map, page));
        }else {
            model.addAttribute("page", blogService.listBlog(page,null));
        }

        System.out.println("=================================================");
        System.out.println("接受到的参数："+blogVo+"当前页码page："+page);

        return "admin/blogs :: blogList";
    }

    @GetMapping("/blogs/{id}/input")
    public String editInput(Model model,@PathVariable Integer id){
        setTypeAndTag(model);
        Blog blog = blogService.findById(id);
        System.out.println("=============================================================================");
        System.out.println("传入thymeleaf的blog；"+blog);
        model.addAttribute("blog",blog);
        return "admin/blogs-input";
    }

    @GetMapping("/blogs/input")
    public String input(Model model){
        setTypeAndTag(model);
        Blog blog = new Blog();
        model.addAttribute("blog",blog);
        return "admin/blogs-input";
    }

    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Integer id){
        blogService.deleteById(id);
        return "redirect:/admin/blogs";
    }

    @PostMapping("/blogs")
    public String save(Blog blog,String blogTagIds){
        System.out.println("======================================");
        System.out.println(blogTagIds);
        blog.setColumnist(typeService.findById(blog.getColumnist().getId()));
        blog.setBlogTag(tagService.listByIds(blogTagIds));
        System.out.println("此时的blog："+blog);
        blogService.save(blog);
        return "redirect:/admin/blogs";
    }



    private void setTypeAndTag(Model model){
        model.addAttribute("types",typeService.list());
        model.addAttribute("tagServiceTags",tagService.list());
    }



}
